//
//  QLMUserInfoDetailVC.m
//  Xiaomakuaixiu
//
//  Created by 龚碧容 on 2017/10/11.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "QLMUserInfoDetailVC.h"
#import "QLMUserTableCell.h"
#import "QLMClearCacheVC.h"
#import "JPUSHService.h"

@interface QLMUserInfoDetailVC ()<UITableViewDelegate,UITableViewDataSource>{
    NSArray *imageArray;
    NSArray *tipsArray;
}
@end

@implementation QLMUserInfoDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title=@"设置";
   
    tipsArray=[NSArray arrayWithObjects:@"密码修改",@"手机修改",@"清除缓存",@"系统版本",@"退出登录", nil];
    imageArray=[NSArray arrayWithObjects:@"03",@"05",@"06",@"07",@"08", nil];
    
    UITableView *_tableview=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, kScreen_height-49) style:UITableViewStylePlain];
    _tableview.backgroundColor=[UIColor clearColor];
    _tableview.delegate=self;
    _tableview.dataSource=self;
    _tableview.tableFooterView=[UIView new];
    [self.view addSubview:_tableview];
    
    if (@available(iOS 11.0, *)) {
        _tableview.contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
        self.automaticallyAdjustsScrollViewInsets=NO;
    }
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=NO;
}

#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//- (UIButton *)baocreatButton:(CGRect)aFrame title:(NSString *)aTitle image:(NSString *)imageName{
//    UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    newButton.frame = aFrame;
//    [newButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
//    [newButton setTitle:aTitle forState:UIControlStateNormal];
//    [newButton setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
//    newButton.backgroundColor = [UIColor whiteColor];
//    newButton.titleLabel.font=[UIFont systemFontOfSize:18.0f];
//    return newButton;
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - UITableViewDatasource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, 120)];
    headerView.backgroundColor=[UIColor clearColor];
    headerView.userInteractionEnabled=YES;
    return headerView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return imageArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QLMUserTableCell *usercell=[QLMUserTableCell cellWithTableView:tableView];
    
    if (imageArray.count!=0) {
        usercell.tipsLabel.text=tipsArray[indexPath.row];
        usercell.iconImageView.image=[UIImage imageNamed:imageArray[indexPath.row]];
        if(indexPath.row==3) {
            usercell.versionLabel.hidden=NO;
            usercell.versionLabel.text= [NSString stringWithFormat:@"V%@",[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];
            usercell.detailImageV.hidden=YES;
        }
    }
    
    return usercell;
}
#pragma mark - UITableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row==0) {
        if ([[UserManager sharedManager] isUserLogin]) {
            [self changePassportButtonAction];
        }else{
            [self presentaLoginVC];
        }
    }else if (indexPath.row==1){
        if ([[UserManager sharedManager] isUserLogin]) {
            [self changeMobileVC];
        }else{
            [self presentaLoginVC];
        }
    }else if (indexPath.row==2) {//清除缓存
        QLMClearCacheVC *clearcacheVC=[QLMClearCacheVC new];
        self.hidesBottomBarWhenPushed=YES;
        [self.navigationController pushViewController:clearcacheVC animated:YES];
    }else if (indexPath.row==4){
        if ([[UserManager sharedManager] isUserLogin]) {//判断是否登录
            [self logoutAlert];
        }else{
            ETToast(@"请先登录");
        }
    }
}

- (void)presentaLoginVC{
    QLMLoginViewController *loginController = [QLMLoginViewController new];
    loginController.resultBlock=^(BOOL success){
        [self.navigationController popViewControllerAnimated:NO];
    };
    QLMCustomNavCtr *nav=[[QLMCustomNavCtr alloc] initWithRootViewController:loginController];
    [self.navigationController presentViewController:nav animated:YES completion:nil];
}

- (void)changePassportButtonAction{
    QLMForgetViewController *changeVC=[QLMForgetViewController new];
    changeVC.isBindBool=NO;
    self.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:changeVC animated:YES];
}

- (void)changeMobileVC{
    QLMRegisterViewController *mobileVC=[QLMRegisterViewController new];
    mobileVC.isEditMobile=YES;
    self.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:mobileVC animated:YES];
}

- (void)logoutAlert{
//    __weak QLMUserInfoDetailVC *weakself = self;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确定退出登录？" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                              [self commitClick];
                                                          }];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * action) {
                                                         }];
    [alert addAction:defaultAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)commitClick{
    [[UserManager sharedManager] loginOut];
    [JPUSHService setAlias:nil completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
    } seq:0];
    ETToast(@"退出成功");
    [[NSNotificationCenter defaultCenter] postNotificationName:@"loginUpdateMainUI" object:nil userInfo:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

@end
